package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.base.enums.WhetherEnum;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.leaf.api.service.IdGenServiceRpc;
import com.weixing.mall.provider.api.service.OrderServiceRpc;
import com.weixing.mall.provider.model.domain.Member;
import com.weixing.mall.provider.model.dto.BinCustomerDto;
import com.weixing.mall.provider.model.dto.MemberChgEnableDto;
import com.weixing.mall.provider.model.dto.MemberDto;
import com.weixing.mall.provider.model.query.MemberBindQuery;
import com.weixing.mall.provider.model.query.MemberQuery;
import com.weixing.mall.provider.model.vo.MemberListVo;
import com.weixing.mall.provider.model.vo.MemberVo;
import com.weixing.mall.provider.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wx
 * @Date 2019/9/16 17:08
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("member")
@Api(value = "后端:个人资料Api", tags = "后端:个人资料")
public class AdminMemberController extends BaseController {
    @Autowired
    private IMemberService memberService;
    @Autowired
    private OrderServiceRpc orderServiceRpc;
    @Autowired
    private IdGenServiceRpc idGenServiceRpc;

    @ApiOperation(value = "加载会员列表")
    @GetMapping("")
    public Result<IPage<MemberListVo>> list(MemberQuery memberQuery) {
        IPage<MemberListVo> userIPage = memberService.selectListOfPage(initMpPage(), memberQuery);
        return ResultUtil.success(userIPage);
    }

    @ApiOperation(value = "查询详情")
    @GetMapping("{id}")
    public Result<MemberVo> selectDetail(@PathVariable long id) {
        return ResultUtil.success(memberService.selectDetail(id));
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@RequestBody MemberDto memberDto) {
        Member member = memberService.selectByMobile(memberDto.getMobile());
        if (null == memberDto.getId() &&  null != member) {
            return ResultUtil.error("手机号已被占用");
        }
        memberService.create(memberDto);
        return ResultUtil.success();
    }
    @ApiOperation(value = "保存初始化")
    @GetMapping("saveInit")
    @NoNeedAccess
    public Result saveInit() {
      /* List<Tmp> tmpList=memberService.init();
           int i=143413;
           for(int k=143413;k<tmpList.size();k++){
               Tmp  ttt=tmpList.get(k);
               Member member1=memberService.selectByMobile(ttt.getContactPhone());
               if(null==member1){
                   i++;
                   Member member=new Member();
                   member.setAccumPoint(0L);
                   member.setAgentBindTime(new Date());
                   member.setAgentId(ttt.getAgentId());
                   member.setAgentName(ttt.getAgentName());
                   member.setArea(ttt.getRegion());
                   member.setAreaCode(ttt.getRegionCode());
                   member.setCity(ttt.getCity());
                   member.setCityCode(ttt.getCityCode());
                   member.setHeight(ttt.getHeight());
                   member.setWeight(null==ttt.getWeight()?"":ttt.getWeight().toString());
                   member.setIsAgent(0);
                   member.setGender(3);
                   member.setIsDel(0);
                   member.setIsEnable(1);
                   member.setNickName(ttt.getContactUser());
                   long memberNo = idGenServiceRpc.segmentKey(IdGenTagEnum.MEMBER_NO.getValue());
                   member.setMemberNo("lm_" + memberNo);
                   member.setPassword(CommonUtil.encryptPassword(ttt.getContactPhone().substring(ttt.getContactPhone().length()-6,ttt.getContactPhone().length())));
                   member.setMobile(ttt.getContactPhone());
                   memberService.save(member);
                   System.out.println("插入记录"+i+"条");

               }
       }*/

        return ResultUtil.success();
    }
    @ApiOperation(value = "删除会员信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "会员id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result del(@PathVariable long id) {
        Member member = memberService.getById(id);
        if (WhetherEnum.YES.getValue().equals(member.getIsAgent())){
            return ResultUtil.error("该会员拥有代理权限，不能删除");
        }
        Result<Integer> result = orderServiceRpc.checkMemberOrderByMemberId(id);
        if (!result.isSuccess()){
            return ResultUtil.error("删除失败");
        }
        if (WhetherEnum.YES.getValue().equals(result.getData())){
            return ResultUtil.error("该会员有订单，不允许删除");
        }
        memberService.removeById(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改状态")
    @PostMapping("chgEnable")
    public Result updateIsEnable(@Validated MemberChgEnableDto memberChgEnableDto) {
        Member member = memberService.getById(memberChgEnableDto.getId());
        member.setIsEnable(memberChgEnableDto.getIsEnable().getValue());
        memberService.updateById(member);
        return ResultUtil.success();
    }


    @ApiOperation(value = "客服与客户绑定关系")
    @PostMapping("bindCustomer")
    public Result bindCustomer(@RequestBody BinCustomerDto binCustomerDto) {
        Member m = memberService.getOne(new QueryWrapper<Member>().eq("nick_name", binCustomerDto.getNickName()).eq("mobile", binCustomerDto.getMobile()));
        if (m != null) {
            if (null != m.getAgentId()) {
                return ResultUtil.error("会员已绑定过代理，不能重复绑定");
            }
            binCustomerDto.setMemberId(m.getId());
            memberService.bindCustomer(binCustomerDto);
            return ResultUtil.success();
        }
        return ResultUtil.error("该用户不存在");
    }

    @ApiOperation(value = "客服与客户绑定关系列表")
    @GetMapping("memberBindlist")
    public Result getMemberBindlist(MemberBindQuery memberBindQuery) {
        return ResultUtil.success(memberService.memberBindlist(initMpPage(), memberBindQuery));
    }

    //刪除就是把会员绑定代理商置为空
    @ApiOperation(value = "客服与客户绑定关系-刪除")
    @PostMapping("bindDel/{id}")
    public Result bindDel(@PathVariable Long id) {
        memberService.bindDel(id);
        return ResultUtil.success();
    }

}
